package com.highcom.service.impl;

import com.highcom.mapper.PermissionDao;
import com.highcom.mapper.RoleDao;
import com.highcom.mapper.UserDao;


import com.highcom.model.Permission;
import com.highcom.model.Role;
import com.highcom.model.User;
import com.highcom.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 用户服务
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private PermissionDao permissionDao;

    @Override
    public User findByUserName(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息，不包含用户的角色
        if (user ==null){
            return null;
        }
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);

        }
        user.setRoles(roles);
        return user;
    }
}
